/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import data.UserData;
import data.PrestasiData;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import database.PrestasiDB;

/**
 *
 * @author Muhammad Dony
 */
public class PrestasiUserServlet extends HttpServlet {
UserData listUser;
    PrestasiDB prestasidb;
    HttpSession session;
    private String action;
    PrestasiData listPrestasi;
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    
    
    
    
    protected void halamanPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        session = request.getSession();
        UserData user = (UserData) session.getAttribute("userMasuk");
        List listprestasi = new PrestasiDB().getprestasiById(user.getID());
        request.setAttribute("listprestasi", listprestasi);        
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/view_prestasi.jsp");
        rd.forward(request, response);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // login validation start
        session = request.getSession();
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        if (udata != null) {
           // if (udata.isLogin()) { // if login
                action = request.getParameter("action");
                if (request.getParameter("action") == null) {
                    try {
                        halamanPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("add")) {
                    halamanTambahPrestasi(request, response);
                } else if (action.equals("edit")) {
                    try {
                        halamanEditPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("delete")) {
                    try {
                        deletePrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                else { // if not login
                RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
                rd.forward(request, response);
            }
        } else {
            RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/login_user.jsp");
            rd.forward(request, response);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // get session
        session = request.getSession();
        // mengambil object sesiUser dari session
        Object ud = session.getAttribute("userMasuk");
        UserData udata = (UserData) ud;
        // mengambil object dengan parameter action dari request
        action = request.getParameter("action");

        if (request.getParameter("action") == null) {
                    try {
                        halamanPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Tambah")) {
                    try {
                        tambahPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else if (action.equals("Edit")) {
                    try {
                        editPrestasi(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(PrestasiUserServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                
                }
    }
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
    public void halamanTambahPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("title", "Tambah Prestasi");
        request.setAttribute("buttonName", "Tambah");
        request.setAttribute("listPrestasi", new PrestasiData());
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/add_prestasi.jsp");
        rd.forward(request, response);
    }

    public void halamanEditPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        request.setAttribute("title", "Edit Prestasi");
        request.setAttribute("buttonName", "Edit");
        String ID  = request.getParameter("id");
        int no  = Integer.parseInt(request.getParameter("no"));
        List listprestasi = new PrestasiDB().getprestasiByxx(ID, no);
        PrestasiData dataPrestasi = (PrestasiData) listprestasi.get(0);
        request.setAttribute("dataPrestasi", dataPrestasi);
        RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/User/profil/add_prestasi.jsp");
        rd.forward(request, response);
    }

    public void tambahPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listPrestasi = new PrestasiData();
        // set listUser
        listPrestasi.setID(request.getParameter("ID"));
        listPrestasi.setPrestasi(request.getParameter("prestasi"));
        listPrestasi.setKegiatan(request.getParameter("kegiatan"));
        listPrestasi.setTingkat(request.getParameter("tingkat"));
        listPrestasi.setTahun(request.getParameter("tahun"));
        listPrestasi.setKategori(request.getParameter("kategori"));
        listPrestasi.setStatus(request.getParameter("status"));
        // memasukkannya dalam database
        prestasidb = new PrestasiDB();
        prestasidb.insertPrestasi(listPrestasi);
        // redirect ke halaman pelaporan
        halamanPrestasi(request, response);
    }

    public void editPrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        // membuat object listUser
        listPrestasi = new PrestasiData();
        // set listUser
        listPrestasi.setNomor(Integer.parseInt(request.getParameter("no")));
        listPrestasi.setPrestasi(request.getParameter("prestasi"));
        listPrestasi.setKegiatan(request.getParameter("kegiatan"));
        listPrestasi.setTingkat(request.getParameter("tingkat"));
        listPrestasi.setTahun(request.getParameter("tahun"));
        listPrestasi.setKategori(request.getParameter("kategori"));
        listPrestasi.setStatus(request.getParameter("status"));
        // memasukkannya dalam database
        prestasidb = new PrestasiDB();
        prestasidb.updatePrestasi(listPrestasi);
        // redirect ke halaman pelaporan
        halamanPrestasi(request, response);
    }

    
    //ambilnya dari url di bagian view
    public void deletePrestasi(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, SQLException, ClassNotFoundException {
        String ID = request.getParameter("id");
        int no  = Integer.parseInt(request.getParameter("no"));
        prestasidb = new PrestasiDB();
        prestasidb.deletePrestasi(ID, no);
        halamanPrestasi(request, response);
    }
    
}
